<?php

namespace App\Service\Pay;

use App\Enums\Check\CheckEnum;
use App\Enums\OpenShop\InvestmentPromotion\ErrCode;
use App\Enums\OpenShop\InvestmentPromotion\OpenShopCacheEnum;
use App\Exceptions\Custom\CustomHttpResponseException;
use App\Library\Rms\RmsPayApi;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderPayHistoryModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSpaceDesignCostMappingModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSpaceDesignSoftOutfitMappingModel;
use App\Service\BaseService;
use Illuminate\Support\Facades\Cache;

class PayParamService extends BaseService
{
    private string $orderNo;
    private int    $sysType;
    private int    $payChannel;
    private int    $payAction;
    private string $h5BackUrl;
    private int    $amount;
    private string $subject;
    private string $bizId;
    private int    $shopId;
    private int    $currentUid;

    public function __construct(
        string $orderNo,
        int $sysType,
        int $payChannel,
        int $payAction,
        string $h5BackUrl,
        int $amount,
        string $subject,
        string $bizId,
        int $shopId,
        int $currentUid
    ) {
        $this->orderNo    = $orderNo;
        $this->sysType    = $sysType;
        $this->payChannel = $payChannel;
        $this->payAction  = $payAction;
        $this->h5BackUrl  = $h5BackUrl;
        $this->amount     = $amount;
        $this->subject    = $subject;
        $this->bizId      = $bizId;
        $this->shopId     = $shopId;
        $this->currentUid = $currentUid;
    }

    /**
     * @return string
     */
    public function getOrderNo(): string
    {
        return $this->orderNo;
    }

    /**
     * @return int
     */
    public function getSysType(): int
    {
        return $this->sysType;
    }

    /**
     * @return int
     */
    public function getPayChannel(): int
    {
        return $this->payChannel;
    }

    /**
     * @return int
     */
    public function getPayAction(): int
    {
        return $this->payAction;
    }

    /**
     * @return string
     */
    public function getH5BackUrl(): string
    {
        return $this->h5BackUrl;
    }

    /**
     * @return int
     */
    public function getAmount(): int
    {
        return $this->amount;
    }

    /**
     * @return string
     */
    public function getSubject(): string
    {
        return $this->subject;
    }

    /**
     * @return string
     */
    public function getCurrentUid(): int
    {
        return $this->currentUid;
    }

    /**
     * wap支付操作
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function prepareWapPay(): array
    {
        // 加一个10秒的锁
        if (Cache::has(OpenShopCacheEnum::getLockWapPay($this->sysType, $this->orderNo))) {
            throw new CustomHttpResponseException(
                ErrCode::errCodeTrans(ErrCode::CODE_WAP_PAY_LOCK)
            );
        }
        Cache::put(OpenShopCacheEnum::getLockWapPay($this->sysType, $this->orderNo), 1, 10);

        // 新增一条支付流水
        $addPayLogId = OpenShopSignOrderPayHistoryModel::addHistory(
            $this->orderNo,
            $this->amount,
            $this->amount,
            $this->payChannel,
            OpenShopSignOrderPayHistoryModel::TYPE_FINAL,
            $this->subject,
            $this->sysType,
            $this->bizId
        );

        $response = (new RmsPayApi($this->sysType))
            ->wapPay($addPayLogId, $this->subject, $this->amount, $this->payChannel, $this->h5BackUrl, $this->shopId, $this->bizId);
        if (!isset($response['code']) || !$response['code']) {
            throw new CustomHttpResponseException(
                ErrCode::errCodeTrans(ErrCode::CODE_ERR_WAKE_PAY)
            );
        }

        $payNo = $response['data']['payNo'] ?? '';
        if ($payNo) {
            OpenShopSignOrderPayHistoryModel::updatePayNo($addPayLogId, $payNo);
        }

        $response['data']['wapPayData']['payNo'] = (string)$payNo;

        return $response['data']['wapPayData'] ?? [];
    }

    /**
     * app支付操作
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function prepareAppPay(): array
    {
        // 新增一条支付流水
        $addPayLogId = OpenShopSignOrderPayHistoryModel::addHistory(
            $this->orderNo,
            $this->amount,
            $this->amount,
            $this->payChannel,
            OpenShopSignOrderPayHistoryModel::TYPE_FINAL,
            $this->subject,
            $this->sysType,
            $this->bizId,
            OpenShopSignOrderPayHistoryModel::PAY_STATUS_WAIT,
            0,
            '',
            $this->currentUid
        );

        $response = (new RmsPayApi($this->sysType))
            ->appPay($addPayLogId, $this->subject, $this->amount, $this->payChannel, $this->shopId, $this->bizId);
        if (!isset($response['code']) || !$response['code']) {
            throw new CustomHttpResponseException(
                ErrCode::errCodeTrans(ErrCode::CODE_ERR_WAKE_PAY)
            );
        }

        $payNo = $response['data']['payNo'] ?? '';
        if ($payNo) {
            OpenShopSignOrderPayHistoryModel::updatePayNo($addPayLogId, $payNo);
        }

        $response['data']['payNo'] = (string)$payNo;

        return $response['data'] ?? [];
    }
}
